<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
  <meta name="generator" content=
  "HTML Tidy for Linux/x86 (vers 1 September 2005), see www.w3.org">

  <title>copy</title>
  <link href="../cppreference.css" rel="stylesheet" type="text/css">
</head>

<body>
<table>
  <tr>
  <td>
  <div class="body-content">

  <div class="header-box">
    <a href="../index.html">cppreference.com</a> &gt; <a href=
    "index.html">C++ Strings</a> &gt; <a href="copy.html">copy</a>
  </div>

  <div class="name-format">
    copy
  </div>

  <div class="syntax-name-format">
    Syntax:
  </div>
  <pre class="syntax-box">
  #include &lt;string&gt;
  <strong>size_type</strong> copy( char* str, <strong>size_type</strong> num, <strong>size_type</strong> index = 0 );
</pre>

  <p>The copy() function copies <em>num</em> characters of the current
  string (starting at <em>index</em> if it&#39;s specified, 0
  otherwise) into <em>str</em>.</p>

  <p>The return value of copy() is the number of characters copied.</p>

  <p>For example, the following code uses copy() to extract a substring
  of a string into an array of characters:</p>
  <pre class="example-code">
 char buf[30];
 memset( buf, &#39;\0&#39;, 30 );
 string str = &quot;Trying is the first step towards failure.&quot;;
 str.copy( buf, 24 );
 cout &lt;&lt; buf &lt;&lt; endl;               
</pre>

  <p>When run, this code displays:</p>
  <pre class="example-code">
 Trying is the first step               
</pre>

  <p>Note that before calling copy(), we first call (Standard C String
  and Character) <a href="../stdstring/memset.html">memset</a>() to
  fill the destination array with copies of the <strong>NULL</strong>
  character. This step is included to make sure that the resulting
  array of characters is <strong>NULL</strong>-terminated.</p>

  <div class="related-name-format">
    Related topics:
  </div>

  <div class="related-content">
    <a href="substr.html">substr</a>
  </div>
  </div>
  </td>
  


  </tr>
  </table>
</body></html>
